<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="粉丝id" prop="fanId">
      <el-input v-model="dataForm.personName" placeholder="粉丝id" style="width: 420px;">
        <el-button slot="append" icon="el-icon-search" @click="fansCheckHandle">选择</el-button>
      </el-input>
    </el-form-item>
    <el-form-item label="门店" prop="deptId">
      <el-select v-model="dataForm.deptId" placeholder="请选择" style="width: 420px;">
        <el-option v-for="item in saleDeptOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="兑换商品" prop="giftId">
      <el-select v-model="dataForm.giftId" placeholder="请选择" style="width: 420px;">
        <el-option v-for="item in giftOptions" :key="item.id" :label="item.giftName" :value="item.id"></el-option>
      </el-select>
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-switch v-model="dataForm.status" active-text="保存并审核" inactive-value="0" active-value="1" style="margin-left: 15px;"></el-switch>
    </span>

    <search-fans v-if="fansVisible" ref="fansDialog" @refreshForm="getFansData"></search-fans>
  </el-dialog>
</template>

<script>
    import {getFansExchangeLogObj, addFansExchangeLogObj, putFansExchangeLogObj} from '@/api/mp/fansexchangelog'
    import {getDeptInfoByDeptType} from '@/api/admin/dept'
    import {getWxGiftList} from '@/api/mp/wxgift'

    import SearchFans from './components/CheckFans'
    export default {
    data () {
      return {
        visible: false,
        fansVisible: false,
        saleDeptOptions: [],
        giftOptions: [],
        dataForm: {
          id: 0,
          fanId: '',
          deptId: '',
          status: ''
        },
        dataRule: {
          fanId: [
            { required: true, message: '粉丝id不能为空', trigger: 'blur' }
          ],
          deptId: [
            { required: true, message: '门店id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '兑换状态(1-已兑换,0-未兑换)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      SearchFans
    },
    methods: {
      init (id) {
        for(let key in this.dataForm){
          if(key == 'status'){
            this.dataForm[key] = '1'
          }else{
            this.dataForm[key] = ''
          }
        }
        this.dataForm.id = id || 0
        this.visible = true
        this.getSaleDeptList(),
        this.getGiftList()
        this.$nextTick(() => {
          if (this.dataForm.id) {
            getFansExchangeLogObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 打开选择粉丝弹窗
      fansCheckHandle(){
        this.fansVisible = true
        this.$nextTick(() => {
          this.$refs.fansDialog.init()
        })

      },
      // 获取粉丝数据
      getFansData(data){
        this.$set(this.dataForm,'fanId',data.id)
        this.$set(this.dataForm,'personName',data.personName)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putFansExchangeLogObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addFansExchangeLogObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      },
      //获取销售门店信息
      getSaleDeptList () {
        getDeptInfoByDeptType(1).then(result => {
          this.saleDeptOptions = result.data.data
        })
      },
      //获取兑换商品信息
      getGiftList(){
        getWxGiftList().then(result => {
          this.giftOptions = result.data.data
        })
      }
    }
  }
</script>
